package com.sporteamws;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity 
{
	public final static String AUTH = "authentication";
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(com.sporteamws.R.layout.c2dm);
        SetPolicies();
        
        /* tmp ----- just for test*/
        Button bTester = (Button)findViewById(R.id.bTmpTester);
        Button bMichael = (Button)findViewById(R.id.bTmpMichael);
        
        final String fIDtester = "100003730751502";
        final String fIDMichael = "853435462";
        bTester.setOnClickListener(new View.OnClickListener()
        {
			public void onClick(View v) 
			{
               MessageUtil.SendMsgToUser(fIDtester, "HiTester");
			}
		});
        
        bMichael.setOnClickListener(new View.OnClickListener()
        {
			public void onClick(View v) 
			{
				MessageUtil.SendMsgToUser(fIDMichael, "HiMichael");
			}
		});
    }
    
    // Main c2dm registration function
    /*  The intent include an extra with the key "sender" and the email address which was registered for the C2DM service
     *  It also must include PendingIntent with the "app" extra. The PendingIntent gives the Android system information 
     *  about the current application. The value for the "sender" is the email address under which we registered to C2DM  */
	public void register(View view)
	{
		Log.w("C2DM", "!---start registration process---!");
		Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
		intent.putExtra("app",PendingIntent.getBroadcast(this, 0, new Intent(), 0));
		intent.putExtra("sender", "sporteamapp@gmail.com");
		startService(intent);
	}

	// show registration ID(authorization token)
	public void showRegistrationId(View view) 
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		String regID = prefs.getString(AUTH, "registration id is missing");
		Toast.makeText(this, regID, Toast.LENGTH_LONG).show();
		Log.w("showing registration id = ", regID);
	}
	
     //set policies for android 4 and up (for use of network IO from main UI thread)
	 public void SetPolicies()
	 {
		 /* 
	        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
	        .detectDiskReads()
	        .detectDiskWrites()
	        .detectNetwork()   // or .detectAll() for all detectable problems
	        .penaltyLog()
	        .build());
	        
	         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
	        .detectLeakedSqlLiteObjects()
	        .detectLeakedSqlLiteObjects()
	        .penaltyLog()
	        .penaltyDeath()
	        .build()); */
	 }
	 
	//save registration ID in main preferences
	private void saveRegistrationId(Context context, String registrationId) 
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		Editor edit = prefs.edit();
		edit.putString(MainActivity.AUTH, registrationId);
		edit.commit();
	}

	//create notification upon successful registration
	public void createNotification(Context context, String registrationId) 
	{
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(com.sporteamws.R.drawable.ic_launcher,"Registration successful", System.currentTimeMillis());
		// Hide the notification after its selected
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		Intent intent = new Intent(context, RegistrationResultActivity.class);
		intent.putExtra("registration_id", registrationId);
		PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
		notification.setLatestEventInfo(context, "C2DM Registration","Successfully registered!", pendingIntent);
		notificationManager.notify(0, notification);
	}

	
	
}